﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsOrderManagement
{
    public partial class ModForm : Form
    {
        public ModForm()
        {
            InitializeComponent();
            LoadData1();
            LoadData2();
        }


        private void label2_Click(object sender, EventArgs e)
        {//donoting 
        }

        //确定修改
        private void button1_Click(object sender, EventArgs e)
        {   //检查订单编号
            string s = textBox1.Text;

            //格式正确且能找到：
            if ((int.TryParse(s, out int orderId)) && (orderId >= 0 && orderId <= initial.OrderID))
            {//进行修改：
                var existingOrders =initial.orderService.QueryByOrderId(s);
                var existingOrder = existingOrders[0];
                //这里找到订单之后在dataGridView里面加颜色显示，并且跑到第一行方便看
                //下面进行信息修改：
                {
                    //ComboBox1选没选？选了更新订单编号的客户，否则用原量。
                    if (comboBox1.SelectedIndex !=-1)//1,2,3
                    {
                        string selectedText1 = comboBox1.Text;
                        existingOrder.Customer = initial.customers[int.Parse(selectedText1)-1];
                    }
                    //ComboBox2选没选？选了更新订单编号的商品
                    if (comboBox2.SelectedIndex != -1)
                    {
                        string selectedText2 = comboBox2.Text;
                        existingOrder.OrderDetails.Product = initial.products[int.Parse(selectedText2) - 1];
                    }
                    //ComboBox3选没选？选了更新订单编号商品的数量
                    if(textBox2.Text!="")
                    {
                        int quantity = int.Parse(textBox2.Text);
                        existingOrder.OrderDetails.Quantity = quantity;
                    }
                }

                //更新数据：
                ModForm_Load( sender, e);
                //清空：
                textBox2.Clear();
            }
            //订单编号不对
            else
            {
                ModErrorForm mod = new ModErrorForm();
                mod.ShowDialog();
            
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
        }

        private void ModForm_Load(object sender, EventArgs e)
        {
            // 将 Order 列表转换为扁平化的匿名对象列表
            var data = initial.orderService.orders
                .Select(o => new
                {
                    OrderID = o.OrderId,
                    CustomerName = o.Customer.Name,
                    OrderDate = o.OrderDate,
                    ProductName = o.OrderDetails.Product.NAME,
                    Quantity = o.OrderDetails.Quantity,
                    Price = o.OrderDetails.Product.Price
                })
                .ToList();

            // 清空所有现有列（包括手动添加的）
            dataGridView1.Columns.Clear();

            // 允许自动生成列
            dataGridView1.AutoGenerateColumns = true;

            // 绑定到 DataGridView
            dataGridView1.DataSource = data;

            // 可选：调整列标题和格式
            dataGridView1.Columns["OrderID"].HeaderText = "订单号";
            dataGridView1.Columns["CustomerName"].HeaderText = "客户";
            dataGridView1.Columns["OrderDate"].HeaderText = "日期";
            dataGridView1.Columns["ProductName"].HeaderText = "商品";
            dataGridView1.Columns["Quantity"].HeaderText = "数量";
            dataGridView1.Columns["Price"].HeaderText = "单价";
            dataGridView1.Columns["OrderDate"].DefaultCellStyle.Format = "yyyy-MM-dd";
        }
        private void LoadData1()
        {    // 清空所有现有列（包括手动添加的）
            dataGridView2.Columns.Clear();

            // 允许自动生成列
            dataGridView2.AutoGenerateColumns = true;

            //把product的List绑定到dataGridView1
            dataGridView2.DataSource = initial.products;

            dataGridView2.Columns["ID"].HeaderText = "商品编号";
            dataGridView2.Columns["NAME"].HeaderText = "商品";
            dataGridView2.Columns["Price"].HeaderText = "商品单价（元/公斤）";
        }
        private void LoadData2()
        {
            // 清空所有现有列（包括手动添加的）
            dataGridView3.Columns.Clear();

            // 允许自动生成列
            dataGridView3.AutoGenerateColumns = true;
            //把customers的List绑定到dataGridView2
            dataGridView3.DataSource = initial.customers;

            dataGridView3.Columns["ID"].HeaderText = "客户编号";
            dataGridView3.Columns["Name"].HeaderText = "名字";
            dataGridView3.Columns["Contact"].HeaderText = "联系方式";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form1 formFromMod = new Form1();
            this.Close();
        }
    }
}
